<template>
  <div>
    <router-link to="/index/admin/add">
      <el-button>添加</el-button>
    </router-link>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="uid" label="用户ID" width="180"></el-table-column>
      <el-table-column prop="id" label="ID" width="180"></el-table-column>
      <el-table-column prop="rolename" label="角色名称" width="180">
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
          <el-tag v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            circle
            size="mini"
            @click="edit(scope.row.uid)"
            icon="el-icon-edit"
          ></el-button>
          <el-button
            type="danger"
            circle
            size="mini"
            @click="del(scope.row.id)"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- size-change：修改每页显示多少条时的处理 -->
    <!-- current-change：页面跳转时的处理 -->
    <!-- current-page：当前页码 -->
    <!-- page-size：每页显示多少条 -->
    <!-- layout：显示哪些控件 -->
    <!-- total：总条数 -->
     <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-size="pageSize"
      layout="prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
import {getTotalAdminNumer, getUserlist} from '../../request'
export default {
  data() {
    return {
      tableData: [],
      // 当前页码
      currentPage: 1,
      // 总条数
      total: 0,
      // 每页显示多少条
      pageSize: 2
    };
  },
  created() {
    // 获取管理员总条数
    this.getNumber();
  },
  methods: {
    // 获取表格的数据
    async getData() {
      let res = await getUserlist({
        size: this.pageSize,
        page: this.currentPage
      })
      this.tableData = res.list;
    },
    // 获取总条数
    async getNumber() {
      let res = await getTotalAdminNumer();
      this.total = res.list[0].total;
      //  total÷pageSize，向上取整，得出总页码，用户点击页码时修改currentPage
      this.getData();
    },
    // 编辑
    edit(uid) {
      this.$router.push("/index/role/" + uid);
    },
    // 删除,需要先弹窗二次确认
    del(id) {
      this.$confirm("确认关闭？")
        .then((_) => {
          // 确认删除
          this.doDel(id);
        })
        .catch((_) => {
          // 关闭弹窗
        });
    },
    doDel(id) {
      this.$http.post("/roledelete", { id }).then((res) => {
        this.tableData = res.list;
      });
    },
    // 切换页面
    handleCurrentChange(val) {
      this.getData();
    }
  },
};
</script>
<style lang="">
</style>